package com.example.mybaitsdemo.dao;

import com.example.mybaitsdemo.bean.User;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserDao {
    DBAccess dbAccess = new DBAccess();
    public List<User> queryUserList() {
        List<User> userList = new ArrayList<>();
        SqlSession sqlSession = null;
        try {
            sqlSession = dbAccess.getSqlSession();
            userList = sqlSession.selectList("selectAll");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        return userList;
    }
    public User queryUser(int id) {
        User user=null;
        SqlSession sqlSession = null;
        try {
            sqlSession = dbAccess.getSqlSession();
            user = sqlSession.selectOne("selectByPrimaryKey", id);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        return user;
    }
}
